/*
 * @Author: Wilbur
 * @Date: 2019-03-21 14:34:36
 * @Last Modified by: Wilbur
 * @Last Modified time: 2019-03-22 18:45:23
 */

<template>
    <div class="container right-create-wrap">
        <el-form
            v-if="init"
            ref="mainform"
            :model="formData"
            label-position="right"
            status-icon
            :rules="formRule"
            label-width="110px"
            :disabled="false">
            <!-- 权益设置 -->
            <div class="form-unit">
                <div class="form-title">权益设置</div>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    prop="type_no"
                    label="权益分类:">
                    <el-select
                        v-model="formData.type_no"
                        class="task-input"
                        placeholder="请选择权益分类">
                        <el-option
                            v-for="(item) in rightTypeList"
                            :key="item.type_no"
                            :label="item.type_name"
                            :value="item.type_no"/>
                    </el-select>
                </el-form-item>
                <el-form-item
                    label="权益状态:">
                    <div>
                        <el-radio
                            size="small"
                            v-model="formData.benefit_status"
                            :label="0">未上线</el-radio>
                        <el-radio
                            size="small"
                            v-model="formData.benefit_status"
                            :label="1">已上线</el-radio>
                    </div>
                </el-form-item>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    prop="benefit_name"
                    label="权益名称:">
                    <el-input
                        placeholder="必填"
                        v-model="formData.benefit_name"
                        size="small"
                        class="task-input"/>
                        <!-- <span style="display:inline-block;color:#999;margin-left:12px">*赞助商名称字数不超过15字</span> -->
                </el-form-item>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    prop="benefit_cover"
                    label="权益图片:">
                    <el-upload
                        class="avatar-uploader-customer"
                        action="https://jsonplaceholder.typicode.com/posts/"
                        :show-file-list="false"
                        :http-request="e=> {fnUploadImageOfBenefitCover(e)}"
                        accept="image/*">
                        <img
                            v-if="formData.benefit_cover"
                            :src="formData.benefit_cover"
                            class="avatar">
                        <i
                            v-else
                            class="el-icon-plus avatar-uploader-icon"/>
                    </el-upload>
                    <span style="display:inline-block;color:#999;margin-left:12px">*只能上传jpg/png文件</span>
                </el-form-item>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    prop="benefit_price"
                    label="权益价值:">
                    <el-input
                        placeholder="必填"
                        v-model="formData.benefit_price"
                        size="small"
                        type="number"
                        class="task-input"/>
                </el-form-item>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    prop="benefit_price_origin"
                    label="原价:">
                    <el-input
                        v-model="formData.benefit_price_origin"
                        size="small"
                        class="task-input"/>
                </el-form-item>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    prop="benefit_price_discount"
                    label="会员价:">
                    <el-input
                        v-model="formData.benefit_price_discount"
                        size="small"
                        class="task-input"/>
                </el-form-item>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    prop="benefit_info"
                    label="权益简介:">
                    <el-input
                        placeholder="必填"
                        v-model="formData.benefit_info"
                        size="small"
                        type="textarea"
                        :rows="5"
                        class="task-input"/>
                    <span style="display:inline-block;color:#999;margin-left:12px">*简介不超过500字</span>
                </el-form-item>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    prop="benefit_sort"
                    label="权益排序:">
                    <el-input
                        placeholder="必填"
                        v-model="formData.benefit_sort"
                        size="small"
                        type="number"
                        class="task-input"/>
                        <!-- <span style="display:inline-block;color:#999;margin-left:12px">*标题字数不超过20字</span> -->
                </el-form-item>

                <el-form-item
                    label="按钮类型:">
                    <div>
                        <el-radio
                            size="small"
                            v-model="formData.button_type"
                            label="none">无按钮</el-radio>
                        <el-radio
                            size="small"
                            v-model="formData.button_type"
                            label="link">链接</el-radio>
                        <el-radio
                            size="small"
                            v-model="formData.button_type"
                            label="image">弹窗</el-radio>
                    </div>
                </el-form-item>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    prop="button_name"
                    v-if="formData.button_type == 'link' || formData.button_type == 'image'"
                    label="按钮名称:">
                    <el-input
                        placeholder="必填"
                        v-model="formData.button_name"
                        size="small"
                        class="task-input"/>
                        <!-- <span style="display:inline-block;color:#999;margin-left:12px">*标题字数不超过20字</span> -->
                </el-form-item>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    prop="button_link"
                    v-if="formData.button_type == 'link'"
                    label="按钮链接:">
                    <el-input
                        placeholder="必填"
                        v-model="formData.button_link"
                        size="small"
                        class="task-input"/>
                        <!-- <span style="display:inline-block;color:#999;margin-left:12px">*标题字数不超过20字</span> -->
                </el-form-item>
                <el-form-item
                    class="task-form-item"
                    size="small"
                    prop="button_image"
                    v-if="formData.button_type == 'image'"
                    label="弹窗图片:">
                    <el-upload
                        class="avatar-uploader-customer"
                        action="https://jsonplaceholder.typicode.com/posts/"
                        :show-file-list="false"
                        :http-request="e=> {fnUploadImageOfButtonImage(e)}"
                        accept="image/*">
                        <img
                            v-if="formData.button_image"
                            :src="formData.button_image"
                            class="avatar">
                        <i
                            v-else
                            class="el-icon-plus avatar-uploader-icon"/>
                    </el-upload>
                    <span style="display:inline-block;color:#999;margin-left:12px">*只能上传jpg/png文件</span>
                </el-form-item>
            </div>

            <div class="form-unit">
                <div class="operate-baar">
                    <el-button
                        size="medium"
                        :disabled="benefitNo ? true : false"
                        @click="fnReset()">重置</el-button>
                    <el-button
                        size="medium"
                        type="primary"
                        @click="fnSubmit()">提交</el-button>
                </div>
            </div>
        </el-form>
    </div>
</template>

<script>
import ueditor from '@/components/common/ueditor.vue';
import regin from '@/assets/js/regin.min.js';
export default {
    components: { ueditor },
    data() {
        var validateAwardMoney = (rule, val, callback) => {

            if(!this.formData.award_money_max){
                callback(new Error('请填写最大赞助金额'));
                return;
            }
            if (Number(this.formData.award_money_min) > Number(this.formData.award_money_max)) {
                callback(new Error('赞助金额填写错误'));
                return ;
            }
            callback();
        };

        // 验证二位数组,且数组内元素不为空
        var validateArray = (rule, value, callback) => {
            if (!value) {
                callback(new Error(rule.message));
            } else {
                if (value.length < 2 || !value[0] || !value[1]) {
                    callback(new Error(rule.message));
                    return ;
                }
                callback();
            }
        };

        return {
            authResult: {},
            breadcrumbList: [
                {
                    title: "广告系统",
                },
                {
                    title: "权益列表",
                    name: 'rightList',
                },
                {
                    title: '创建权益'
                }
            ],
            benefitNo: '',
            provence: [],
            formData: {
                type_no: '',
                benefit_name: '',
                benefit_cover: '',
                benefit_price: '',
                benefit_info: '',
                button_type: 'none',
                benefit_sort: '',
                benefit_status: 1,
                button_name: '',
                button_link: '',
                button_image: '',
                benefit_price_origin: '',
                benefit_price_discount: ''
            },
            formRule: {
                type_no: [
                    { required: true, message: '请选择权益分类', trigger: 'change' }
                ],
                benefit_name: [
                    { required: true, message: '请输入权益名称', trigger: 'blur' },
                    // { min: 1, max: 15, message: '长度在 1 到 15 个字', trigger: 'blur' }
                ],
                benefit_cover: [
                    { required: true, message: '请上传权益图片', trigger: 'change' }
                ],
                benefit_price: [
                    { required: true, message: '请输入权益价格', trigger: 'blur' }
                ],
                benefit_info: [
                    { required: true, message: '请输入权益简介', trigger: 'blur' },
                    { min: 1, max: 500, message: '长度在 1 到 500 个字', trigger: 'blur' }
                ],
                benefit_sort: [
                    { required: true, message: '请输入权益排序', trigger: 'blur' },
                ],
                button_name: [
                    { required: true, message: '请输入按钮名称', trigger: 'blur' },
                ],
                button_link: [
                    { required: true, message: '请输入按钮跳转链接', trigger: 'blur' },
                ],
                benefit_image: [
                    { required: true, message: '请上传权益图片', trigger: 'change' }
                ],
                award_money_max: [
                    { validator: validateAwardMoney, trigger: 'blur' }
                ]
            },
            submitting: false,
            init: false,
            baseUrl: '',
            sessionTicket: localStorage.getItem('sessionTicket'),
            escape: escape,
            rightTypeList: []
        };
    },
    created() {
        this.authResult = this.$route.meta.authResult;
        this.$store.commit('SET_HEADER', this.breadcrumbList);

        this.benefitNo = this.$route.query.benefit_no ? this.$route.query.benefit_no : '';

        if (!this.benefitNo) {
            this.init = true;
        }else{
            this.fnGetRightInfo();
        }
        this.fnGetRightTypeList();
    },
    methods: {
        fnInitTimeNow() {
            var timestamp=new Date().getTime();

            return timestamp;
        },
        fnGetRightInfo() {
            this.$post('/wei4/benefit%5Cget', {
                benefit_no: this.benefitNo
            }, resp => {
                if(resp.code == 1){
                    this.formData = {
                        ...resp.data,
                        button_link: '',
                        button_name: '',
                        button_image: ''
                    };
                    if(resp.data.button_type == 'link') {
                        this.formData.button_link = resp.data.button_data.url;
                        this.formData.button_name = resp.data.button_data.title;
                    }else if(resp.data.button_type == 'image') {
                        this.formData.button_image = resp.data.button_data.url;
                        this.formData.button_name = resp.data.button_data.title;
                    }
                    this.init = true;
                }else{
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },
        fnGetRightTypeList() {
            this.$post('/wei4/benefit_type%5Cget_list', {}, resp => {
                if(resp.code == 1){
                    this.rightTypeList = resp.data.list;
                }else{
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },
        fnSubmit(){
            if(this.submitting){
                return ;
            }
            this.$refs.mainform.validate(valid => {
                if(valid){
                    this.submitting = true;

                    let data = {
                        type_no: this.formData.type_no,
                        benefit_name: this.formData.benefit_name,
                        benefit_cover: this.formData.benefit_cover,
                        benefit_price: this.formData.benefit_price,
                        benefit_info: this.formData.benefit_info,
                        button_type: this.formData.button_type,
                        benefit_sort: this.formData.benefit_sort,
                        benefit_status: this.formData.benefit_status,
                        benefit_price_discount: this.formData.benefit_price_discount,
                        benefit_price_origin: this.formData.benefit_price_origin
                    };

                    if(this.formData.button_type == 'none') {
                        data.button_json = '{}';
                    }else if(this.formData.button_type == 'link') {
                        data.button_json = JSON.stringify({
                            title: this.formData.button_name,
                            url: this.formData.button_link
                        });
                    }else if(this.formData.button_type == 'image') {
                        data.button_json = JSON.stringify({
                            title: this.formData.button_name,
                            url: this.formData.button_image
                        });
                    }

                    if(this.benefitNo){
                        data.benefit_no = this.benefitNo;
                    }

                    this.$post('/wei4/benefit%5Csave', data, resp => {
                        if(resp.code == 1){
                            this.$message({
                                type: 'success',
                                center: true,
                                message: '操作成功'
                            });
                            this.$router.push({name: 'rightList'});
                        }else{
                            this.$message({
                                type: 'warning',
                                center: true,
                                message: resp.msg
                            });
                        }
                        this.submitting = false;
                    });
                }
            });
        },
        fnUploadImageSuccess(e, type) {
            if(type == 'cover') {
                console.log('successsss');
                return {name: 'type', status: true};
            }
        },
        /** 数组中删除某个元素 */
        fndeleteItem(list, index, bind) {
            list.splice(index, 1);
            this[bind] = list.length;
        },
        /** 上传组件回调 文件队列修改 */
        fnUploadChange(file, item) {
            item.preview_url = file.url;
        },
        fnBeforeUpload(file, item){
            item.status = 'uploading';
        },
        /** 上传组件回调 上传成功 */
        fnUploadSuccess(resp, file, item) {
            item.img_url = resp.url;
            item.status = 'success';
        },
        fnUploadError(item){
            item.preview_url = '';
            item.status = 'fail';
        },
        fnUpload(e, item) {
            let timeTmp = new Date().getTime();
            let pic = new FormData();

            pic.append('file', e.file);
            item.uploadTime = timeTmp;
            this.$upload({
                url: '/__local/common%5Cupload',
                data: pic,
                progress: e.onProgress,
                success: resp => {
                    if(item.uploadTime != timeTmp){
                        return ;
                    }
                    if (resp.code == 1) {
                        // 调用组件成功回调,修改样式
                        e.onSuccess(resp.data, {
                            url: resp.data.url
                        }, this.fileList);
                    } else {
                        // 失败后调用组件error回调,删除对应元素
                        e.onError(resp.msg);
                        this.$message({
                            type: 'warning',
                            message: '上传图片失败',
                            center: true
                        });
                    }
                }
            });
        },
        fnUploadImage(e, success, error) {
            let timeTmp = new Date().getTime();
            let pic = new FormData();

            pic.append('file', e.file);
            this.$upload({
                url: '/__local/common%5Cupload',
                data: pic,
                success: resp => {
                    if (resp.code == 1) {
                        success(resp);
                    } else {
                        this.$message({
                            type: 'warning',
                            message: '上传图片失败',
                            center: true
                        });
                        error(resp);
                    }
                }
            });
        },
        fnUploadImageOfBenefitCover(e) {
            this.fnUploadImage(e, (resp) => {
                this.formData.benefit_cover = resp.data.url;
            }, (resp) => {
                console.log(resp.msg);
            });
        },
        fnUploadImageOfButtonImage(e) {
            this.fnUploadImage(e, (resp) => {
                console.log(resp.data.url, '1');
                this.formData.button_image = resp.data.url;

                console.log(this.formData.button_image, '2');
            }, (resp) => {
                console.log(resp.msg);
            });
        },
        fnReset(){
            this.$refs.mainform.resetFields();
        }
    },
};
</script>

<style lang="less" scoped>
    .margin-top{
        margin-top: 15px;
    }
    .right-create-wrap {
      background: #fff;
      margin: 15px;
      margin-bottom: 0;
      padding: 30px;
      overflow: auto;
    }
    .form-unit {
      margin-bottom: 40px;
    }
    .form-title {
      font-size: 16px;
      font-weight: 600;
      color: rgba(0, 0, 0, 0.85);
      line-height: 18px;
      padding: 0 10px;
      border-left: 2px solid #1890ff;
      margin-bottom: 20px;
    }
    .task-input {
      width: 435px !important;
    }
    .money-line{
        width: 35px;
        text-align: center;
    }
    .money-wrap{
        display: inline-block;
        width: 200px;
    }
    .money-input{
        width: 200px!important;
    }
    .task-intro-input {
      width: 750px;
    }
    .table-inner-input {
      outline: none;
      line-height: 20px;
      font-size: 14px;
      width: 100%;
      background: transparent;
      text-align: center;
      cursor: pointer;
    }
    input::-webkit-outer-spin-button,
    input::-webkit-inner-spin-button {
      -webkit-appearance: none;
    }
    input[type="number"] {
      -moz-appearance: textfield;
    }
    .input-width-delete {
      margin-top: 15px;
      font-size: 0;
      .task-input{
          margin-right: 10px;
      }
    }

    .operate-baar {
      width: 100%;
      text-align: center;
      margin-top: 168px;
    }
    .preview-image {
      width: 120px;
      height: 120px;
      background-position: center;
      background-size: contain;
      background-repeat: no-repeat;
    }

    .pic-width-delete {
      margin-top: 15px;
      float: left;
      margin-right:60px;
      display: inline-block;
      width: 500px;
      display: flex;
      justify-content: flex-start;
      align-items: flex-start;
      .pic-unit {
        flex: 1;
        margin-right: 10px;
        display: flex;
        justify-content: flex-start;
        align-items: flex-start;
        padding: 15px;
        border-radius: 4px;
        border: 1px solid rgba(217, 217, 217, 1);
        &-index {
          width: 28px;
          height: 28px;
          background: rgba(243, 250, 255, 1);
          border: 1px solid rgba(24, 144, 255, 1);
          margin-right: 15px;
          border-radius: 28px;
          text-align: center;
          line-height: 28px;
          color: rgba(24, 144, 255, 1);
        }
        &-detail{
            flex: 1;
        }
        &-desc{
            width:100%;
        }
        &-textarea{
            resize: none;
            width: 100%;
            height: 44px;
            line-height: 20px;
            border: 0;
            outline: 0;
        }
        .pic-value {
          width:100%;
          height: 215px;
          background-position: center;
          background-size: contain;
          background-repeat: no-repeat;
          background-color: #F6F6F6;
          position: relative;
          .loading{
              width:100%;
              height: 100%;
              line-height: 215px;
              background: rgba(0,0,0,.3);
              transition: opacity .3s ease-in-out;
          }
          .tip{
              font-size: 14px;
              color: #fff;
              line-height: 215px;
              width: 100%;
              height:100%;
              background: rgba(0,0,0,.5);
              position: absolute;
              left: 0;
              top: 0;
              opacity: 0;
              transition: opacity .3s ease-in-out;
          }
          &:hover{
              .loading{
                  opacity: 0;
              }
              .tip{
                  opacity: 1;
              }
          }
        }
        .pic-empty-container{
            display: flex;
            width: 100%;
            height: 100%;
            flex-direction: column;
            justify-content: center;
            align-items: center;
        }
        .pic-empty-plus{
            font-size:24px;
            width: 24px;
            line-height: 24px !important;
            color: rgba(0,0,0,.45);
        }
        .pic-empty-desc{
            font-size:14px;
            color:rgba(0,0,0,0.65);
            line-height:22px;
        }
      }
    }
    .upload-list{
        margin-top:10px;
    }
    .editor-wrap{
        position: relative;
    }
    .uploaded-image{
        width: 360px;
        height: 180px;
        background-size: 100%;
        background-position: center center;
        background-repeat: no-repeat;
        &:hover{
            .icon-delete{
                display: inline-block;
            }
        }
        .icon-delete{
            margin-top: 90px;
            transform: translateY(-50%);
            display: none;
        }
    }
</style>
<style lang="less">
    .avatar-uploader-customer{
        display: inline-block;
        .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 100px;
            height: 100px;
            line-height: 100px;
            text-align: center;
        }
        .avatar {
            width: 100px;
            height: 100px;
            display: block;
        }
    }
    .provence-list{
        .el-checkbox {
            margin-left: 0;
            margin-right: 30px;
        }
    }
</style>
